From 676c3642ac023387e2bef248fa3ae58d2a87afd0 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Sat, 11 Jan 2020 13:04:49 -0700 Subject: [PATCH] fix potential memory leaks in ggv_ovl. (#456) * fix potential memory leaks in ggv_ovl. * ggv_ovl control flow and string processing tweaks. --- ggv_ovl.cc | 40 ++++++++++++++++++---------------------- inifile.cc | 6 +++--- inifile.h | 4 ++-- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/ggv_ovl.cc b/ggv_ovl.cc index 4ba623973..7eb70d84c 100644 --- a/ggv_ovl.cc +++ b/ggv_ovl.cc @@ -21,9 +21,9 @@ */ #include // for sin, cos, acos -#include // for snprintf #include // for QString +#include // for QVector #include // for foreach #include "defs.h" @@ -101,10 +101,9 @@ ggv_ovl_read() { int symbols = inifile_readint_def(inifile, "Overlay", "Symbols", -1); - for (int i = 1; i <= symbols; i++) { - char symbol[32]; + for (int i = 1; i <= symbols; ++i) { - snprintf(symbol, sizeof(symbol), "Symbol %d", i); + QString symbol = QString("Symbol %1").arg(i); OVL_SYMBOL_TYP type = (OVL_SYMBOL_TYP) inifile_readint_def(inifile, symbol, "Typ", 0); int points = inifile_readint_def(inifile, symbol, "Punkte", -1); @@ -113,7 +112,6 @@ ggv_ovl_read() QString lon; switch (type) { - char coord[32]; Waypoint* wpt; int group; @@ -138,25 +136,23 @@ ggv_ovl_read() trk->rte_name = QString("Track %1").arg(track_ct); } - for (int j = 0; j < points; j++) { + for (int j = 0; j < points; ++j) { wpt = new Waypoint; - snprintf(coord, sizeof(coord), "YKoord%d", j); - lat = inifile_readstr(inifile, symbol, coord); - if (!lat.isNull()) { - wpt->latitude = lat.toDouble(); - } else { + lat = inifile_readstr(inifile, symbol, QString("YKoord%1").arg(j)); + if (lat.isNull()) { + delete wpt; continue; } + wpt->latitude = lat.toDouble(); - snprintf(coord, sizeof(coord), "XKoord%d", j); - lon = inifile_readstr(inifile, symbol, coord); - if (!lon.isNull()) { - wpt->longitude = lon.toDouble(); - } else { + lon = inifile_readstr(inifile, symbol, QString("XKoord%1").arg(j)); + if (lon.isNull()) { + delete wpt; continue; } + wpt->longitude = lon.toDouble(); if (group > 1) { route_add_wpt(rte, wpt); @@ -174,17 +170,17 @@ ggv_ovl_read() wpt->shortname = symbol; lat = inifile_readstr(inifile, symbol, "YKoord"); - if (!lat.isNull()) { - wpt->latitude = lat.toDouble(); - } else { + if (lat.isNull()) { + delete wpt; continue; } + wpt->latitude = lat.toDouble(); lon = inifile_readstr(inifile, symbol, "XKoord"); - if (!lon.isNull()) { - wpt->longitude = lon.toDouble(); - } else { + if (lon.isNull()) { + delete wpt; continue; } + wpt->longitude = lon.toDouble(); waypt_add(wpt); break; diff --git a/inifile.cc b/inifile.cc index 0c385c230..624f779ac 100644 --- a/inifile.cc +++ b/inifile.cc @@ -204,9 +204,9 @@ inifile_done(inifile_t* inifile) } bool -inifile_has_section(const inifile_t* inifile, const char* section) +inifile_has_section(const inifile_t* inifile, const QString& section) { - return inifile->sections.contains(QString(section).toLower()); + return inifile->sections.contains(section.toLower()); } /* @@ -248,7 +248,7 @@ inifile_readint(const inifile_t* inifile, const QString& section, const QString& */ int -inifile_readint_def(const inifile_t* inifile, const char* section, const char* key, const int def) +inifile_readint_def(const inifile_t* inifile, const QString& section, const QString& key, const int def) { int result; diff --git a/inifile.h b/inifile.h index f428caa93..5706775af 100644 --- a/inifile.h +++ b/inifile.h @@ -39,7 +39,7 @@ struct inifile_t { inifile_t* inifile_init(const QString& filename, const char* myname); void inifile_done(inifile_t* inifile); -bool inifile_has_section(const inifile_t* inifile, const char* section); +bool inifile_has_section(const inifile_t* inifile, const QString& section); /* inifile_readstr: @@ -59,6 +59,6 @@ int inifile_readint(const inifile_t* inifile, const QString& section, const QStr inifile_readint_def: if found inifile_readint_def returns value of key, otherwise a default value "def" */ -int inifile_readint_def(const inifile_t* inifile, const char* section, const char* key, int def); +int inifile_readint_def(const inifile_t* inifile, const QString& section, const QString& key, int def); #endif -- 2.30.2